<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/19
 * Time: 下午6:00
 */

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Moment extends Base
{
    public $table = 'moment';
    public $moment_like_table = 'moment_like';
    public $comment_table = 'moment_comment';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 新增动态
     * @return mixed
     */
    public function addMoment($data){
        $res = DB::table($this->table)->insertGetId($data);

        return $res;
    }

    /**
     * 获取动态列表
     * @return mixed
     */
    public function getMomentListBySearch($where, $pageNo, $pageSize, $orderBy="id desc"){
        if($pageNo<1) $pageNo = 1;
        if($pageSize<1) $pageSize = 20;

        $query = DB::table($this->table)->where('is_deleted', 0);
        if(!empty($where['place'])) {
            $query = $query->where('place', $where['place']);
        }
        if(!empty($where['user_id'])) {
            $query = $query->where('user_id', $where['user_id']);
        }
        if(!empty($where['id'])) {
            $query = $query->where('id', $where['id']);
        }

        $query = $query->orderByRaw($orderBy)->offset(($pageNo-1)*$pageSize)->limit($pageSize);
        $data = $query->get()->toArray();
        $data = self::objToArray($data);
        foreach($data as $key=>$val) {
            if(!empty($val['images'])) {
                $images = json_decode($val['images'], true);
                if(!empty($images)) {
                    $data[$key]['images'] = [];
                    foreach($images as $img) {
                        $data[$key]['images'][] = build_url($img);
                    }
                }else{
                    $data[$key]['images'] = [];
                }
            }
        }

        return $data;
    }

    /**
     * 获取动态信息
     * @return mixed
     */
    public function getMomentInfo($momentId){
        if(empty($momentId)) {
            return [];
        }
        $query = DB::table($this->table)->where('is_deleted', 0)->where('id', $momentId);
        $data = $query->first();
        $data = self::objToArray($data);
        if(!empty($data['images'])) {
            $images = json_decode($data['images'], true);
            if(!empty($images)) {
                $data['images'] = [];
                foreach($images as $img) {
                    $data['images'][] = build_url($img);
                }
            }else{
                $data['images'] = [];
            }
        }

        return $data;
    }

    /**
     * 新增动态点赞信息
     * @return mixed
     */
    public function addMomentLike($userId, $momentId){
        $res = DB::table($this->moment_like_table)->insertGetId(['user_id'=>$userId, 'moment_id'=>$momentId]);

        return $res;
    }

    /**
     * 更新动态点赞信息
     * @return mixed
     */
    public function updateMomentLike($userId, $momentId, $status){
        $res = DB::table($this->moment_like_table)
            ->where('user_id', $userId)
            ->where('moment_id', $momentId)
            ->where('is_deleted', 0)
            ->update(["status"=>$status]);

        return $res;
    }

    /**
     * 获取动态点赞信息
     * @return mixed
     */
    public function getMomentLikeByMomentId($userId, $momentId){
        $data = DB::table($this->moment_like_table)
            ->where('user_id', $userId)
            ->where('moment_id', $momentId)
            ->where('is_deleted', 0)
            ->first();
        $data = self::objToArray($data);
        return $data;
    }

    /**
     * 更新动态点赞信息
     * @return mixed
     */
    public function incrMomentLikeNum($momentId, $field, $num){
        if(empty($momentId) || empty($num)) {
            return false;
        }
        $res = DB::table($this->table)
            ->where('id', $momentId)
            ->increment($field, $num);

        return $res;
    }

    /**
     * 获取动态点赞列表
     * @return mixed
     */
    public function getMomentLikeListBySearch($where, $pageNo, $pageSize, $orderBy="id desc"){
        if($pageNo<1) $pageNo = 1;
        if($pageSize<1) $pageSize = 20;

        $query = DB::table($this->moment_like_table)->where('is_deleted', 0);
        if(!empty($where['user_id'])) {
            $query = $query->where('user_id', $where['user_id']);
        }
        if(!empty($where['moment_id'])) {
            $query = $query->where('moment_id', $where['moment_id']);
        }
        if(!empty($where['status'])) {
            $query = $query->where('status', $where['status']);
        }

        $query = $query->orderByRaw($orderBy)->offset(($pageNo-1)*$pageSize)->limit($pageSize);
        $data = $query->get()->toArray();
        $data = self::objToArray($data);

        return $data;
    }

    /**
     * 新增动态评论
     * @return mixed
     */
    public function addComment($data){
        $res = DB::table($this->comment_table)->insertGetId($data);

        return $res;
    }

    /**
     * 获取动态评论列表
     * @return mixed
     */
    public function getCommentListBySearch($where, $pageNo, $pageSize, $orderBy="id desc"){
        if($pageNo<1) $pageNo = 1;
        if($pageSize<1) $pageSize = 20;

        $query = DB::table($this->comment_table)->where('is_deleted', 0);
        if(!empty($where['user_id'])) {
            $query = $query->where('user_id', $where['user_id']);
        }
        if(!empty($where['moment_id'])) {
            $query = $query->where('moment_id', $where['moment_id']);
        }
        if(!empty($where['place'])) {
            $query = $query->where('place', $where['place']);
        }

        $query = $query->orderByRaw($orderBy)->offset(($pageNo-1)*$pageSize)->limit($pageSize);
        $data = $query->get()->toArray();
        $data = self::objToArray($data);

        return $data;
    }

    /**
     * 获取树洞收取信件列表
     * @return mixed
     */
    public function getTreeholeIncomeList($userId){
        $sql = "SELECT * FROM (SELECT id,user_id,content,like_num,comment_num,created_at FROM zb_moment WHERE is_deleted=0 AND place=2 AND user_id!=".$userId."  AND id NOT IN (SELECT moment_id FROM zb_moment_comment WHERE user_id=".$userId." AND is_deleted=0) GROUP BY user_id) as a LIMIT 8";
        $data = DB::select($sql);
        $data = self::objToArray($data);

        return $data;
    }

    /**
     * 批量获取动态信息
     * @return mixed
     */
    public function getMomentInfoByIds($momentIds)
    {
        $list = DB::table($this->table)->whereIn('id', $momentIds)->get()->toArray();
        $list = $this->objToArray($list);
        $list = array_column($list, null, 'id');

        return $list;
    }

}